<template>
	<web-view ref="webViewRef" @message="handlePostMessage" src="http://shop.gluz.com.cn/ts/#/pages/index/index"></web-view>
</template>
<script>
	var wv;
	import {
		createSendMessage,
		createC2CConversation
	} from "@/common/public";
	export default {
		data() {
			return {
				statusBarHeight: 0,
				nameType: false,
			}
		},
		mounted() {
			let statusBarHeight = JSON.stringify(uni.getSystemInfoSync().statusBarHeight);
			let statusbottom = JSON.stringify(uni.getSystemInfoSync().safeAreaInsets.bottom);
			let lang = uni.getStorageSync('lang');
			let userId = uni.getStorageSync('user_id');
			let xzdToken = uni.getStorageSync('xzdToken');
			let mobile = uni.getStorageSync('mobile');
			let data = {
				statusBarHeight: statusBarHeight,
				lang: lang,
				userId: userId,
				token: xzdToken,
				mobile: mobile,
				statusbottom: statusbottom
			}
			this.$nextTick(() => {
				this.isSetParams(data);
			})
		},
		methods: {
			isSetParams(data) {
				let currentWebview = this.$scope.$getAppWebview();
				try {
					wv = currentWebview.children()[0];
					wv.addEventListener('loaded', function() {
						wv.evalJS(`setParams(${JSON.stringify(data)})`);
					})

				} catch (e) {
					//TODO handle the exception
					console.log("e=============", e)
				}

			},
			goback() {
				uni.switchTab({
					url: '/pages/tabbar/index'
				});
			},
			async handlePostMessage(data) {
				console.log("接收到消息：" + JSON.stringify(data.detail.data));
				if (data.detail.data[0].name === "goback") {
					this.goback();
				}
				if (data.detail.data[0].name === "login") {
					if (!!this.nameType) return;
					this.nameType = true;
					uni.redirectTo({
						url: "/pages/user/login/login"
					})
					this.nameType = false;
				}
				if (data.detail.data[0].name === "call") {
					let tel = data.detail.data[0].tel;
					// createC2CConversation("15607593523", this)
					createC2CConversation(tel, this)
				}
				if (data.detail.data[0].name === "meeting") {
					let tel = data.detail.data[0].tel;
					let text = tel + (lang !== 'ru' ? "向你申请进行视频会议，请前往多语管家创建会议并邀请" :
						"отправляет вам запрос на проведение видеоконференции, перейдите в раздел Многоязычный управляющий, чтобы создать конференцию и пригласить") + tel;
					// createSendMessage("15607593523", text, this)
					createSendMessage(tel, text, this)
				}
			},
		}
	}
</script>

<style scoped lang="less">
	@import url(@/common/common.less);

	.right_title {
		font-size: 26rpx;
		line-height: 1.2;
		color: #262728;
		margin-right: 10rpx;
	}

	.common_content {
		padding: 0 24rpx;
	}
</style>